// 主界面
;define(function(require, exports, module) {
    'use strict';
    var util = require("util");
    var base = require(CONFIG.BasePath + "js/enterprise/base");
    var router = require("../hash/pageRouter");
    var constant = require("constant");
    var css_letter = require("./accountFinanceContract.css");
    var databind = require("databind");
    var financeContractId;
    var pageIndex = 1,
		pageSize = 8,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
    
    var init = function(){
    	financeContractId = base.getData("financeContractId");
    	databind.fillInput($("#financeContractInfo"), {action:"inquireFinanceContractDetail.json?financeContractId="+financeContractId,async:false});
    	showMaterials();
    	router.init();
		initEvent();
    };
    
	var initEvent = function(){
		if(base.getData("sid")==null){
			location.href="../login/login.html";
		}
		
		$("#showFinanceContractInfo").unbind("click").click(function(){
			$(this).css("color", "#ff7344");
			$(this).parent().css("border-bottom" ,"2px solid #ff7344");
			$(this).parent().parent().find(".span2").css("border-bottom" ,"2px solid #ced4e0");
			$(this).parent().parent().find(".span2 a").css("color" ,"#474e5d");
			$("#financeContractMaterials").addClass("none");
			$("#financeContractInfo").removeClass("none");
		});
		
		$("#showMaterials").unbind("click").click(function(){
			$(this).css("color", "#ff7344");
			$(this).parent().css("border-bottom" ,"2px solid #ff7344");
			$(this).parent().parent().find(".span1").css("border-bottom" ,"2px solid #ced4e0");
			$(this).parent().parent().find(".span1 a").css("color" ,"#474e5d");
			$("#financeContractMaterials").removeClass("none");
			$("#financeContractInfo").addClass("none");
		});
    };
    
    //显示融资材料
    var showMaterials = function(){
    	var str = $(".m").html();
    	str = str.split(",");
    	$.each(str, function(i,v) {
    		var s = '<a target="_blank" href="/finance/api/v1.0/downloadFile.file?fileUuid='+v+'"><img src="../../../res/png/file_icon.png" /></a>';
    		$("#financeContractMaterials").append(s);
    	});
    };
    
    //获得用户信息
    var inquireFinanceContractDetail = function(){
		base.fnAjax({
			action: "inquireFinanceContractDetail.json",
			async: false,
			oData: {
                financeId: financeId
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                	juicer.register("getValueByKey", constant.getValueByKey);
                	var tpl = $("#accountDetailTpl").html();
                	var html = juicer(tpl, data);
                	$("#accountDetailBody").html(html);
                } else {
                }
            }
		});
	};
	
    var inquireLetterListReceived = function(){
		base.fnAjax({
			action: "inquireLetterListReceived.json",
			async: false,
			oData: {
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                	juicer.register("getValueByKey", constant.getValueByKey);
                	var tpl = $("#accountLetterTpl").html();
                	var html = juicer(tpl, data);
                	$("#accountLetterBody").html(html);
                	$(".letterItem").unbind("click").click(function(){
                		markLetterReadReceived($(this).attr("letterId"));
                		$(this).find(".readStatus").html("【已读】");
                		markLetterReadReceived($(this).attr("letterId"));
                	});
//              	totalPageCount = data.totalPageCount;
//					totalRecordCount = data.totalRecordCount;
//              	base.pageControl($("#changepage"),
//						totalRecordCount,
//						data.pageIndex,
//						data.pageSize,
//						function(p) {
//							pageIndex = p;
//							inquireFinanceApplicationList();
//						});
                } else {
                }
            }
		});
	};
	
	//站内信标记为已读
    var markLetterReadReceived = function(letterId){
		base.fnAjax({
			action: "markLetterReadReceived.json",
			async: false,
			oData: {
                letterId: letterId
            }, //传递参数
            fnSuccess: function(data) {
                if (data.status == 200) {
                } else {
                }
            }
		});
	};
	
    return {
        init:init
    };
});